package com.example.androidproject.app.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.androidproject.R;
import com.example.androidproject.app.classifybean.ClassifyBean;
import com.example.androidproject.utils.ReplaceBlank;

import java.util.List;
import java.util.concurrent.CountDownLatch;

public class ClassifyOptionsAdapter extends BaseAdapter {
    private final String TAG = "GGSTUDY";
    private final Context mContext;
    private final List<ClassifyBean.DataBean> data;
    private final int classifyFlag;
    private final TextView btn_classify_options_all1;
    public int mSelect = -1;   //选中项

//    private final CountDownLatch latch = new CountDownLatch(1);
//    private final CountDownLatch requestLatch = new CountDownLatch(1);

    public ClassifyOptionsAdapter(Context context, List<ClassifyBean.DataBean> data, int classifyFlag, TextView btn_classify_options_all1) {
        this.mContext = context;
        this.data = data;
        this.classifyFlag = classifyFlag;
        this.btn_classify_options_all1 = btn_classify_options_all1;
    }

    public void changeSelect(int position) {
        if (mSelect != position) {
            mSelect = position;
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position).getClassify();
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.classify_options_item, null);
            viewHolder.btn_classify_options = convertView.findViewById(R.id.btn_classify_options);
            viewHolder.ll_options = convertView.findViewById(R.id.ll_options);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        String claStr = data.get(position).getClassify();
        claStr = ReplaceBlank.replaceBlank(claStr);
        viewHolder.btn_classify_options.setText(claStr);
        if (mSelect == position) {
            btn_classify_options_all1.setBackgroundResource(R.drawable.unselect_btn_css);
            viewHolder.btn_classify_options.setBackgroundResource(R.drawable.btn_corners_css);
        } else {
            viewHolder.btn_classify_options.setBackgroundResource(R.drawable.unselect_btn_css);
        }
        return convertView;
    }

    public class ViewHolder {
        TextView btn_classify_options;
        LinearLayout ll_options;
    }
}
